<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片内容区域 -->
    <el-card>
      <el-row :gutter="15" style="margin-bottom: 15px">
        <el-col :span="8"
          ><el-input
            placeholder="请输入内容"
            v-model="paramInfo.query"
            clearable
            @clear="getList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getList()"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="listData" style="width: 100%" border>
        <el-table-column prop="goods_id" label="商品ID" width="80">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column
          prop="goods_number"
          label="商品数量"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品单价"
          width="100"
        ></el-table-column>
        <el-table-column prop="upd_time" label="更新时间" width="190">
          <template slot-scope="scope">
            <div>
              {{ scope.row.upd_time | dateFormat }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <div>
              <el-button type="primary" size="mini">编辑</el-button>
              <el-button
                type="danger"
                size="mini"
                @click="removeById(scope.row.goods_id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paramInfo.pagenum"
        :page-sizes="[20, 200, 300, 400]"
        :page-size="paramInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      /* 表格数据对象 */
      listData: [],
      /* 查询商品列表参数 */
      paramInfo: {
        query: "",
        pagenum: 1,
        pagesize: 20,
      },
      total: 0,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /* 获取商品列表 */
    async getList() {
      const { data: res } = await this.$http.get("/goods", {
        params: this.paramInfo,
      });
      if (res.meta.status != 200) {
        return this.$message.error("获取商品列表失败！");
      }
      this.listData = res.data.goods;
      this.total = res.data.total;
      console.log(res.data);
    },
    /* 获得当前页面数据条数 */
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.paramInfo.pagesize = pageSize;
      this.getList();
    },
    /* 获取当前页 */
    handleCurrentChange(currentPage) {
      console.log(currentPage);
      this.paramInfo.pagenum = currentPage;
      this.getList();
    },
    /* 根据ID删除商品 */
    async removeById(id) {
      const confirmResult = await this.$confirm(
        "您确定永久删除该数据吗？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((error) => error);
      if (confirmResult !== "confirm") {
        return this.$message.info("取消了删除！");
      }
      const { data: res } = await this.$http.delete("/goods/" + id);
      if (res.meta.status !== 200) {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除成功");
      this.getList();
    },
    /* 添加页面 */
    goAddPage() {
      this.$router.push("/goods/add");
    },
  },
};
</script>
<style lang="less" scoped></style>
